package com.ibm.csr.staf;

import com.ibm.staf.STAFException;
import com.ibm.staf.STAFHandle;
import com.ibm.staf.STAFResult;

public class TCKickOff {

	private String workDir = "C:/jcc3_test/jcc";

	public TCKickOff(String host) {
		kickhost = host;
	}
	
	public TCKickOff(String host, String dir) {
		kickhost = host;
		workDir = dir;
	}

	private static STAFHandle stafhandle;

	private String kickhost;

	/**
	 * Register STAF
	 */
	public static void RegisterSTAF() {
		try {
			stafhandle = new STAFHandle("STAF_TC_Kick_Off");
		} catch (STAFException e) {
			MSGLog.log("Error registering with STAF, RC: " + e.rc);
			e.printStackTrace();
		}
	}

	/**
	 * Unregister STAF
	 */
	public static void unRegisterSTAF() {
		try {
			stafhandle.unRegister();
		} catch (STAFException e) {
			MSGLog.log("Error unregistering with STAF, RC: " + e.rc);
			System.exit(1);
		}
	}

	/**
	 * Invoke pingClient(String host) to verify whether the kick off host is
	 * alive
	 * 
	 * @return
	 */
	public boolean pingClient() {
		boolean status = pingClient(kickhost);
		return status;
	}

	/**
	 * Ping host to verify whether the host is alive
	 * 
	 * @param host
	 * @return
	 */
	public boolean pingClient(String host) {
		boolean status = false;
		STAFResult result = null;
		result = stafhandle.submit2(kickhost, "PING", "PING");
		if (result.rc == 0 && result.result.compareTo("PONG") == 0) {
			status = false;
		}
		return status;
	}

	/**
	 * Execute String command with STAF <HOST> PROCESS
	 * 
	 * @param command
	 * @return
	 */
	public STAFResult executeCommand(String command) {
		STAFResult result = null;
		MSGLog.log("START SHELL COMMAND \"" + command + "\" WORKDIR "
				+ workDir + " WAIT RETURNSTDOUT RETURNSTDERR");
		result = stafhandle.submit2(kickhost, "PROCESS",
				"START SHELL COMMAND \"" + command + "\" WORKDIR " + workDir
						+ " WAIT RETURNSTDOUT RETURNSTDERR SAMECONSOLE");
		return result;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TCKickOff.RegisterSTAF();
		TCKickOff tc = new TCKickOff("db2csw03.svl.ibm.com");
		STAFResult r = tc
				.executeCommand("c:/java60/bin/java -cp c:/jcc_home/jcc/lib/db2jcc4.jar;.;C:/STAF/bin/JSTAF.jar;C:/STAF/samples/demo/STAFDemo.jar;C:/jcc3_test/jcc/classes;C:/jcc3_test/jcc;C:/jcc_home/jcc/lib/db2jcc.jar;C:/jcc_home/jcc/lib/db2jccharness.jar;C:/jcc_home/jcc/lib/sqlj.zip;C:/jcc_home/jcc/lib/db2jcc_license_cisuz.jar;C:/jcc_home/jcc/lib/j2ee.jar;.;c:/jcc_home/jcc/lib/jdom.jar;C:/jcc_home/jcc/manual/t4xatests/JNDI_JARFILES/fscontext.jar;C:/jcc_home/jcc/manual/t4xatests/JNDI_JARFILES/jndi.jar;C:/jcc_home/jcc/manual/t4xatests/JNDI_JARFILES/providerutil.jar;C:/STAF/services/log4j-1.2.8.jar;C:/PROGRA~1/IBM/SQLLIB/java/db2java.zip;C:/PROGRA~1/IBM/SQLLIB/java/db2jcc.jar;C:/PROGRA~1/IBM/SQLLIB/java/sqlj.zip;C:/PROGRA~1/IBM/SQLLIB/java/db2jcc_license_cu.jar;C:/PROGRA~1/IBM/SQLLIB/bin;C:/PROGRA~1/IBM/SQLLIB/java/common.jar;c:/jcc_home/jcc/src/com/ibm/db2/jcc/harness/fvt/ffvt/lib/edtftpj.jar;c:/jcc_home/jcc/src/com/ibm/db2/jcc/harness/fvt/ffvt/lib/jaxen-core.jar;c:/jcc_home/jcc/src/com/ibm/db2/jcc/harness/fvt/ffvt/lib/jaxen-jdom.jar;c:/jcc_home/jcc/src/com/ibm/db2/jcc/harness/fvt/ffvt/lib/jaxup-1.01.jar;c:/jcc_home/jcc/src/com/ibm/db2/jcc/harness/fvt/ffvt/lib/jdom.jar;c:/jcc_home/jcc/src/com/ibm/db2/jcc/harness/fvt/ffvt/lib/log4j-1.2.15.jar;c:/jcc_home/jcc/src/com/ibm/db2/jcc/harness/fvt/ffvt/lib/saxpath.jar;c:/jcc_home/jcc/src/com/ibm/db2/jcc/harness/fvt/ffvt/lib/xalan.jar;c:/jcc_home/jcc/src/com/ibm/db2/jcc/harness/fvt/ffvt/lib/xml-apis.jar;  com.ibm.db2.jcc.harness.JccHarness -driverType 4 -serverName csrec817.vmec -portNumber 446 -databaseName STLEC1 -user admf001 -password c0deshop -customize no -onlineCheck no -test com.ibm.db2.jcc.harness.fvt.wjdbc02sb.jBasicDML_5 -tracelevel TRACE_NONE");
		MSGLog.log(r.rc + "\nResutl:" + r.result);
		MSGLog.log(r.resultContext.toString());
		MSGLog.log(r.resultContext.marshall());
		KickResult kResult = new KickResult(r);
		System.out
				.println(kResult.rc + "\n" + kResult.outrc + "\n"
						+ kResult.errrc + "\n" + kResult.stdout + "\n"
						+ kResult.stderr);

		TCKickOff.unRegisterSTAF();

	}

}
